package dgo.interval;

public class Parallel extends Interval {
    Interval[] commands;
    public Parallel(Interval[] commands){
      this.commands = commands;      
      int max = 0;
      for(int i = 0; i < commands.length; i++){
        if(commands[i].duration > max){max = commands[i].duration;}
      }
      this.duration = max;
    }

    private void initNode(IntervalNode node){
      for(int i = 0; i < commands.length; i++){
        node.nodes.addElement(new IntervalNode(this));
      }
    }

    public boolean call(IntervalNode node){
      if(node.nodes.isEmpty()){ this.initNode(node); }
      
      boolean b = false;
      
      for(int i = 0; i < commands.length; i++){
        Interval interval = commands[i];
        // if finished, simply check next command
        if(node.counter >interval.duration && interval.duration != -1){ continue; }
        
        // call
        if(interval.call((IntervalNode)node.nodes.elementAt(i))){ b = true; }
      }
      if(b){node.counter++;}
      return b;
    }
}
